//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_CONSOLE_H__
#define __ELASTOS_CONSOLE_H__

#define FLAG_TAB                0x1000

class ConsoleDev : public CDeviceDriver
{
public:
    CARAPI Read(
        /* [in] */ UInt64 u64Offset,
        /* [in] */ UInt32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI ReadKeyboard(uint8_t *p);

    CARAPI WriteToCOM(const MemoryBuf & buffer);

    CARAPI WriteToScreen(
        /* [in] */ UInt64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ UInt32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ UInt64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ UInt32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose() {}

    ECode Initialize();

    ~ConsoleDev()
    {
        if (NULL != m_pKbdDriver) {
            m_pKbdDriver->Release();
        }

        if (NULL != m_pVGADriver) {
            m_pVGADriver->Release();
        }

        if (m_puScreenBuffer) {
            DzFreeKernelPages(m_puScreenBuffer, 2);
        }

        if (m_pWriteBuf) {
            free(m_pWriteBuf);
        }
    }

public:
    IDeviceDriver         *m_pKbdDriver;
    IDeviceDriver         *m_pVGADriver;

    uint32_t        m_uScreenColumn;
    uint32_t        m_uScreenRow;
    uint16_t        *m_puScreenBuffer;

    uint32_t        m_uCursorPosition;

    // The last character on the screen.
    uint32_t        m_uLastPosition;

    char           *m_pWriteBuf;
    Boolean            m_bBrush;
};

#endif // __ELASTOS_CONSOLE_H__
